import Tools from '@/tools/index.js';
import Store from '@/store/index.js';

import VFN from "@/vfn"
import {
    loadMicroApp
} from 'qiankun';
export default {
    // 获取乾坤配置
    appsConfig(type_, name_) {
        Tools.setLoad(true);
        // 开发模式的扩展配置
        let _itemExpand = null;
        Vue.prototype.$AppsExpandConfig.forEach(item => {
            if (item.type == type_ && item.name == name_) {
                _itemExpand = item;
            }
        })

        // qk配置
        let _config = Vue.prototype.$AppsConfig;


       
    
        

        // 微应用的容器节点的选择器或者 Element 实例
        let _container = "";
        // 微应用入口地址
        let _entry = _config.appBaseEntry;
         // 初始化时需要传递给微应用的数据
         let _props = {
            data: {
                mainRoutes: Vue.prototype.$Router,
                $vFN: VFN,
                fromChannel:Tools.getProjectType()
            }
        }

        if (type_ == "apps") {
            let _items = null
            Store.getters.get_appAuthList.forEach(item=>{
                item.list.forEach(iitem=>{
                    if(iitem.appCode==name_){
                        _items = iitem;
                    }
                })
            })
            _props.data.appsInfo = _items
            if(_items.path){
                _entry = _items.path;
            }
            else{
                _entry = _config.appBaseEntry;
            }
            _container = "#qkApps"
        } else {
            // 系统子应用列表
            let _list = Vue.prototype.$AppsProjectList;
            let _items = _list.filter(item => {
                return (item.type == type_ && item.name == name_)
            })
            // 不管存在几个都取最后一个
            let _data = {
                ..._items[_items.length - 1]
            };
            _container = _data.container;
            if (_data.entry) {
                _entry = _data.entry;
            }
        }


        // 是否存在开发配置
        if (_itemExpand) {
            _entry = _itemExpand.entry;
        }
       

        let _model = {
            app: {
                name: type_+"-"+name_,
                entry: _entry + "/" + type_ + '/' + name_ + "/",
                container: _container,
                props: _props
            },
            config: _config.qkConfig
        }
        console.log("apps", {..._model.app})
        return _model
    },
    // 引导程序
    bootstrap(props) {


        console.groupEnd();
        console.group("%c " + props.config.type + "_" + props.config.name + " 挂载-Bootstrap", "background:#7095A4; padding: 1px; border-radius:3px; color: #fff;");
        console.groupEnd();
    },
    beforeMount(props) {

        let _key = props.config.type + '-' + props.config.name

        window.instance[props.key] = {
            config: null,
            app: null,
            router: null,
        }
        console.groupEnd();
        console.group("%c " + props.config.type + "_" + props.config.name + " 初始化-BeforeMount", "background:#7095A4; padding: 1px; border-radius:3px; color: #fff;");
        console.groupEnd();
        VFN.Tools.setLoad(false);
    },
    mount(props) {
        // 如果存在apps的信息
        if(props.params.data.appsInfo){
            VFN.Tools.setDocumentTitle(props.params.data.appsInfo.title);
        }
        
        // console.error("props",props)
        
        Store.dispatch('set_currentApp', props)

        console.groupEnd();
        console.group("%c " + props.config.type + "_" + props.config.name + " 初始化完成-Mount", "background:#7095A4; padding: 1px; border-radius:3px; color: #fff;");
        console.groupEnd();
        VFN.Tools.setLoad(false);

    },

    unmount(props) {

        Store.dispatch('set_currentApp', null)
        console.groupEnd();
        console.group("%c " + props.config.type + "_" + props.config.name + " 卸载-Unmount", "background:#7095A4; padding: 1px; border-radius:3px; color: #fff;");
        console.groupEnd();
        let _key = props.config.type + '-' + props.config.name
        window.instance[_key].app.$destroy();
        window.instance[_key].app.$el.innerHTML = "";
        window.instance[_key].config = null;
        window.instance[_key].router = null;
        window.instance[_key] = null;

    },
    loadMicroApp(app_, config_) {
        return loadMicroApp(app_, config_)
    },

}